package org.resort.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.SerializerFeature;
import org.resort.entity.*;
import org.resort.service.SuperService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

/**
 * @Author 郭文虎
 * <p>
 * 超级管理员模块
 */
@Controller
@RequestMapping("/super")
public class SuperController {
    @Autowired
    SuperService superService;

    /**
     * 跳转至权限管理主页面并拿到所有的父权限
     *
     * @return 权限管理主页面
     */
    @RequestMapping(value = "/privilegeManage.html", method = RequestMethod.GET)
    public String getAllPrivilegeGet(Map<String, Object> map) {
        map.put("parentList", superService.getAllParentPrivilege());
        return "super/privilegeManage";
    }

    /**
     * 权限管理(获取所有权限)
     *
     * @return 所有权限
     */
    @RequestMapping(value = "/privilegeManage.html", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getAllPrivilegePost(@RequestParam("offset") int offset, @RequestParam("limit") int limit, @RequestParam("parentId") int parentId) {
        System.out.println("offset:" + offset);
        System.out.println("limit:" + limit);
        System.out.println("parentId:" + parentId);
        //存储对象
        Map<String, Object> map = new HashMap<String, Object>(16);
        //根据父权限分页获取子权限及当前父权限
        List<Privilege> privilegeList = superService.getPartPrivilegeByPageAndParentId(offset, limit, parentId);
        //满足上述条件的权限数量
        int total = superService.countPartPrivilegeByParentId(parentId);
        map.put("rows", privilegeList);
        map.put("total", total);
        return map;
    }

    /**
     * 根据编号获取一个Privilege对象
     *
     * @param id 编号
     * @return 装有Privilege的Map对象
     */
    @RequestMapping(value = "/getOnePrivilegeById", method = RequestMethod.POST)
    public String getOnePrivilegeById(@RequestParam("id") int id, Map<String, Object> map) {
        map.put("privilege", superService.getOnePrivilege(id));
        return "super/privilegeManageUpdate";
    }

    /**
     * 修改权限
     *
     * @param id     编号
     * @param name   名称
     * @param href   跳转路径
     * @param icon   图标
     * @param parent 父权限
     * @return 受影响行数
     */
    @RequestMapping(value = "/changePrivilege", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> changePrivilege(int id, String name, String href, String icon, String parent) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        int updates = superService.changeOnePrivilegeById(id, name, href, icon, parent);
        map.put("updates", updates);
        return map;
    }

    /**
     * 打开添加权限页面
     *
     * @return 添加权限的页面路径
     */
    @RequestMapping("/openAddPrivilege")
    public String openAddPrivilege() {
        return "super/privilegeManageAdd";
    }

    /**
     * 添加一条权限数据
     *
     * @param name   名称
     * @param href   跳转路径
     * @param icon   图标
     * @param parent 父权限
     * @return 受影响行数
     */
    @RequestMapping("/addPrivilege")
    @ResponseBody
    public Map<String, Object> addPrivilege(String name, String href, String icon, String parent) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        int updates = superService.addPrivilege(name, href, icon, parent);
        map.put("updates", updates);
        return map;
    }

    /**
     * 根据编号删除一条权限
     *
     * @param id 编号
     * @return 受影响行数
     */
    @RequestMapping("/delPrivilegeById")
    @ResponseBody
    public Map<String, Object> delPrivilegeById(int id) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        int updates = superService.delPrivilegeById(id);
        map.put("updates", updates);
        return map;
    }

    /**
     * 打开权限分配页面
     *
     * @return 权限分配页面
     */
    @RequestMapping("/openPrivilegeDistribution.html")
    public String openPrivilegeDistribution() {
        return "super/privilegeDistribution";
    }

    /**
     * 获取所有角色
     *
     * @return 所有角色集合
     */
    @RequestMapping("/getAllRole")
    @ResponseBody
    public Map<String, Object> getAllRole() {
        Map<String, Object> map = new HashMap<String, Object>(16);
        List<Role> roleList = superService.getAllRole();
        map.put("rows", roleList);
        return map;
    }

    /**
     * 根据角色编号获取对应的权限
     *
     * @param roleId 角色编号
     * @return 对应的权限集合
     */
    @RequestMapping("/getPrivilegeByRoleId")
    @ResponseBody
    public Map<String, Object> getPrivilegeByRoleId(@RequestParam("roleId") int roleId) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        List<Privilege> privilegeList = superService.getPrivilegeByRoleId(roleId);
        map.put("rows", privilegeList);
        return map;
    }

    /**
     * 获取某角色所未拥有的权限
     *
     * @param roleId 角色编号
     * @return 这个角色未拥有的权限集合
     */
    @RequestMapping(value = "/getPartPrivilegeNotRoleId", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> getPartPrivilegeNotRoleId(@RequestParam("roleId") int roleId) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        List<Privilege> privilegeList = superService.getPartPrivilegeNotRoleId(roleId);
        map.put("rows", privilegeList);
        return map;
    }

    /**
     * 移除这名角色当前所拥有的一个权限
     *
     * @param roleId      当前角色编号
     * @param privilegeId 将要移除的权限编号
     * @return 受影响行数
     */
    @RequestMapping("/removePrivilege")
    @ResponseBody
    public Map<String, Object> removePrivilege(int roleId, int privilegeId, String level) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        int updates = superService.removePrivilege(roleId, privilegeId, level);
        map.put("updates", updates);
        return map;
    }

    /**
     * 将父权限和对应的子权限一并分配给当前角色
     *
     * @param privilegeId 父权限编号
     * @param roleId      当前角色编号
     * @param level       等级(parent,children)
     * @return 分配权限数量(受影响行数)
     */
    @RequestMapping("/appendPrivilege")
    @ResponseBody
    public Map<String, Object> addPrivilege(int privilegeId, int roleId, String level) {
        System.out.println("privilegeId:" + privilegeId);
        System.out.println("roleId:" + roleId);
        System.out.println("level:" + level);
        Map<String, Object> map = new HashMap<String, Object>(16);
        int updates = superService.appendPrivilege(privilegeId, roleId, level);
        map.put("updates", updates);
        return map;
    }

    /**
     * 打开内部部门管理页面
     *
     * @return 内部部门管理页面
     */
    @RequestMapping("/openInDepartmentManage.html")
    public String openInDepartmentManage() {
        return "super/inDepartmentManage";
    }

    /**
     * 获取所有内部部门信息
     *
     * @return 所有内部部门信息
     */
    @RequestMapping("/getAllInDepartment")
    @ResponseBody
    public Map<String, Object> getAllInDepartment() {
        Map<String, Object> map = new HashMap<String, Object>(16);
        List<InDepartment> inDepartmentList = superService.getAllInDepartment();
        map.put("rows", inDepartmentList);
        return map;
    }

    /**
     * 打开添加内部部门页面
     *
     * @return 内部部门添加页面
     */
    @RequestMapping("/openAddInDepartment.html")
    public String openAddInDepartment() {
        return "super/addInDepartment";
    }


    /**
     * 提交添加的内部部门信息
     *
     * @param inDepartment 将要添加的内部部门对象
     * @return 受影响行数
     */
    @RequestMapping("/submitAddInDepartment")
    @ResponseBody
    public Map<String, Object> submitAddInDepartment(InDepartment inDepartment) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("updates", superService.addOneInDepartment(inDepartment));
        return map;
    }

    /**
     * 打开添加内部部门页面并查询出它的信息
     *
     * @return 内部部门修改页面
     */
    @RequestMapping("/openChangeInDepartment.html")
    public String openChangeInDepartment(int inDepartmentId, Map<String, Object> map) {
        map.put("inDepartment", superService.getOneInDepartmentById(inDepartmentId));
        return "super/changeInDepartment";
    }

    /**
     * 提交添加的内部部门信息
     *
     * @param inDepartment 将要添加的内部部门对象
     * @return 受影响行数
     */
    @RequestMapping("/submitChangeInDepartment")
    @ResponseBody
    public Map<String, Object> submitChangeInDepartment(InDepartment inDepartment) {
        inDepartment.show();
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("updates", superService.changeOneInDepartment(inDepartment));
        return map;
    }

    /**
     * 根据编号删除一个部门
     *
     * @param inDepartmentId 将要删除的部门编号
     * @return 受影响行数
     */
    @RequestMapping("/delInDepartment")
    @ResponseBody
    public Map<String, Object> delInDepartmentById(int inDepartmentId) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("updates", superService.delInDepartmentById(inDepartmentId));
        return map;
    }

    /**
     * 打开外部部门管理页面
     *
     * @return 外部部门管理页面
     */
    @RequestMapping("/openOutDepartmentManage.html")
    public String openOutDepartmentManage() {
        return "super/outDepartmentManage";
    }

    /**
     * 获取所有的外部部门信息
     *
     * @return 所有的外部部门信息
     */
    @RequestMapping("/getAllOutDepartment")
    @ResponseBody
    public Map<String, Object> getAllOutDepartment() {
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("rows", superService.getAllOutDepartment());
        return map;
    }

    /**
     * 根据编号删除外部部门
     *
     * @param outDepartmentId 将要删除的外部部门的编号
     * @return 受影响行数
     */
    @RequestMapping("/delOutDepartment")
    @ResponseBody
    public Map<String, Object> delOutDepartment(int outDepartmentId) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("updates", superService.delOutDepartmentById(outDepartmentId));
        return map;
    }

    /**
     * 打开添加外部部门页面
     *
     * @return 外部部门页面
     */
    @RequestMapping("/openAddOutDepartment.html")
    public String openAddOutDepartment() {
        return "super/addOutDepartment";
    }

    /**
     * 打开会员管理页面
     *
     * @param map 存储对象
     * @return 会员管理页面
     */
    @RequestMapping(value = "/openMemberManage.html", method = RequestMethod.GET)
    public String openMemberManage(Map<String, Object> map) {
        List<State> stateList = superService.getAllMemberState();
        map.put("stateList", stateList);
        List<MemberLevel> levelList = superService.getAllMemberLevel();
        map.put("levelList", levelList);
        return "super/memberManage";
    }

    /**
     * 多条件分页查询会员
     *
     * @param stateId 状态编号
     * @param levelId 等级编号
     * @param name    会员姓名
     * @param offset  偏移量
     * @param limit   第页显示条数
     * @return 满足条件的会员集合
     */
    @RequestMapping(value = "/getMemberByCondition", method = RequestMethod.GET)
    @ResponseBody
    public Map<String, Object> getMemberByCondition(int stateId, int levelId, String name, int offset, int limit) {
        Map map = new HashMap<String, Object>(16);
        List<Member> memberList = superService.getMemberByCondition(stateId, levelId, name, offset, limit);
        map.put("rows", memberList);
        map.put("total", superService.countMemberByCondition(stateId, levelId, name));
        return map;
    }

    /**
     * 打开修改会员页面
     *
     * @return 修改会员页面
     */
    @RequestMapping("/openUpdateMember.html")
    public String openUpdateMember(int memberId, Map<String, Object> map) {
        Member member = superService.getOneMemberById(memberId);
        member.show();
        map.put("member", member);
        map.put("stateList", superService.getAllMemberState());
        map.put("levelList", superService.getAllMemberLevel());
        return "super/updateMember";
    }

    /**
     * 打开添加会员页面
     *
     * @param map 存储对象
     * @return 添加会员页面
     */
    @RequestMapping("/openAddMember.html")
    public String openAddMember(Map<String, Object> map) {
        map.put("levelList", superService.getAllMemberLevel());
        return "super/addMember";
    }

    /**
     * 添加会员表单提交
     *
     * @param member 将要添加的会员对象
     * @return 受影响行数
     */
    @RequestMapping("/submitAddMember")
    @ResponseBody
    public Map<String, Object> submitAddMember(Member member) {
        Map map = new HashMap<String, Object>(16);
        map.put("updates", superService.addOneMember(member));
        return map;
    }

    @RequestMapping("/submitChangeMember")
    @ResponseBody
    public Map<String, Object> submitChangeMember(int id, String name, String identitycard, int memberlevel, String account, double balance, int state, String phone) {
        Map map = new HashMap<String, Object>(16);
        map.put("updates", superService.updateMember(id, name, identitycard, account, balance, phone, memberlevel, state));
        return map;
    }

    /**
     * 打开会员等级管理页面
     *
     * @return 会员等级管理页面
     */
    @RequestMapping("/openMemberLevelManage.html")
    public String openMemberLevelManage() {
        return "super/memberLevelManage";
    }


    /**
     * 根据编号删除一条会员等级
     *
     * @param memberLevelId 将要删除的会员等级编号
     * @return 受影响行数
     */
    @RequestMapping("/delMemberLevel")
    @ResponseBody
    public Map<String, Object> delMemberLevel(int memberLevelId) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("updates", superService.delOneMemberLevel(memberLevelId));
        return map;
    }

    /**
     * 打开添加会员等级页面
     *
     * @return
     */
    @RequestMapping("/openAddMemberLevel.html")
    public String openAddMemberLevel() {
        return "super/addMemberLevel";
    }

    /**
     * 打开修改会员等级页面
     *
     * @param memberLevelId 会员等级编号
     * @param map           存储对象
     * @return
     */
    @RequestMapping("/openChangeMemberLevel.html")
    public String openChangeMemberLevel(int memberLevelId, Map<String, Object> map) {
        map.put("memberLevel", superService.getOneMemberLevelById(memberLevelId));
        return "super/changeMemberLevel";
    }

    /**
     * 获取全部会员等级
     *
     * @return
     */
    @RequestMapping("/getAllMemberLevel")
    @ResponseBody
    public Map<String, Object> getAllMemberLevel() {
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("rows", superService.getAllMemberLevel());
        return map;
    }

    /**
     * 提交添加会员等级
     *
     * @param memberLevel 将要添加的会员等级对象
     * @return 受影响行数
     */
    @RequestMapping("/submitAddMemberLevel")
    @ResponseBody
    public Map<String, Object> submitAddMemberLevel(MemberLevel memberLevel) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        map.put("updates", superService.submitAddMemberLevel(memberLevel));
        return map;
    }

    /**
     * 提交修改会员等级
     *
     * @param memberLevel 将要修改的会员等级对象
     * @return 受影响行数
     */
    @RequestMapping("/submitChangeMemberLevel")
    @ResponseBody
    public Map<String, Object> submitChangeMemberLevel(MemberLevel memberLevel) {
        Map<String, Object> map = new HashMap<String, Object>(16);
        memberLevel.show();
        map.put("updates", superService.submitChangeMemberLevel(memberLevel));
        return map;
    }
}